/**
 * list implementation for CS3251 Project 2.
 *
 *
 * Created and edited by: Nicolas Papin
 **/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "list.h"

/**
 * Creates a userList_t.
 **/
userList_t *create_list()
{
	userList_t *newlist = malloc(sizeof(userList_t));
    if(newlist == NULL)
    {
        return NULL;
    }

    newlist->size = 0;
    newlist->head = NULL;
    newlist->tail = NULL;

	return newlist;
}

/**
 * Pushes a userNode_t into the list at the back.
 *
 * userList_t* list: a pointer to the list
 * userNode_t* node: a pointer to a userNode_t to push
 **/
void addNode(userList_t *list, userNode_t *node)
{
    if(list == NULL || node == NULL)
        return;

    if(list->head == NULL || list->tail == NULL)
    {
        list->head = node;
        list->tail = node;
        node->next = NULL;
    }
    else
    {
        /* Set up node's pointers */
        list->tail->next = node;
        node->next = NULL;

        /* Move the tail pointer */
        list->tail = node;
    }

    ++list->size;
}

/**
 * Removes the userNode_t * from the list.
 *
 * userList_t *list: a pointer to the list.
 * userNode_t *node: a pointer to the node to remove.
 *
 * Returns the userNode_t * from the list or NULL if it was not found.
 **/
userNode_t *removeNode(userList_t* list, userNode_t *node)
{
    userNode_t *curr, *tmp;

    if(list == NULL || list->head == NULL || node == NULL)
        return NULL;

    if(node == list->head)
    {
        list->head = list->head->next;

        if(list->head == NULL)
            list->tail = NULL;

        tmp = node;
    }
    else
    {
        curr = list->head;

        while(curr != NULL && curr->next != node)
            curr = curr->next;

        if(curr != NULL)
        {
            tmp = curr->next;
            curr->next = tmp->next;
            tmp->next = NULL;
        }
        else
            return NULL;
    }

    --list->size;

    return tmp;
}

/**
* Finds a node with the given username in the list and returns it.
*
* userList_t *list - the list to search for the name in.
* const char *name - the username to search the list for.
*
* Return - the usernode if found, null otherwise.
**/
userNode_t *findWithName(userList_t *list, const char *name)
{
    userNode_t *node;

    if(list == NULL || list->head == NULL || name == NULL)
        return NULL;

    node = list->head;

    while(node != NULL)
    {
        if(!strcmp(name, node->uname))
            return node;

        node = node->next;
    }

    return NULL;
}


/**
 * TEST
 * Traverses and prints a linked list from head to tail.
 * (In other words, traverse from head to the last next that's not head again.)
 * Please use your print_node function for printing each node's contents.
 *
 * If the list is empty (i.e., the head pointer is null), please print
 * "EMPTY LIST" and terminate.
 */
/*void traverse_and_print(llist* list)
{
    lnode *current;

    if(list == NULL || list->head == NULL)
    {
        printf("EMPTY LIST\n");
        return;
    }

    current = list->head;

    do
    {
        print_node(current);
        current = current->next;

    }while(current != list->head);
}*/

/**
 * TEST
 * Prints the contents - (x,y) data - as well as the data at previous and
 * next pointers of the given node.
 *
 * A printout for an lnode containing (1,1) with a previous node containing
 * (2,2) and the next node containing (3,3) should look like this:
 *
 Current: (1,1)
 Previous: (2,2)
 Next: (3,3)
 *
 * that is, three lines, each terminated with a newline and not prefixed
 * with comment stars.
 *
 * If the node parameter passed in is a NULL pointer, print the following line:
 print_node error: NULL node parameter
 *
 * You may find it useful to implement a helper function that prints
 * out the data contained within just one node, but this is not
 * mandatory.
 */
/*void print_node(lnode* node)
{
    if(node == NULL)
    {
        printf("print_node error: NULL node parameter\n");
    }
    else
    {
        printf("Current: (%d,%d)\n", node->x, node->y);

        if(node->prev == NULL)
        {
            printf("Previous: NULL\n");
        }
        else
        {
            printf("Previous: (%d,%d)\n", node->prev->x, node->prev->y);
        }

        if(node->next == NULL)
        {
            printf("Next: NULL\n");
        }
        else
        {
            printf("Next: (%d,%d)\n", node->next->x, node->next->y);
        }
    }
}*/
